﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace Dimok {
	public static class DES{

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Encryptions. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="PlainText">	The plain text. </param>
		/// <param name="key">				The key. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static byte[] Encryption(string PlainText, string key) {
			TripleDES des = CreateDES(key);
			ICryptoTransform ct = des.CreateEncryptor();
			byte[] input = Encoding.Unicode.GetBytes(PlainText);
			return ct.TransformFinalBlock(input, 0, input.Length);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Decryptions. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="CypherText">	The cypher text. </param>
		/// <param name="key">				The key. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public static string Decryption(string CypherText, string key) {
			byte[] b = Convert.FromBase64String(CypherText);
			TripleDES des = CreateDES(key);
			ICryptoTransform ct = des.CreateDecryptor();
			byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
			return Encoding.Unicode.GetString(output);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Creates the des. </summary>
		///
		/// <remarks>	Dimon, 11/23/2010. </remarks>
		///
		/// <param name="key">	The key. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		static TripleDES CreateDES(string key) {
			MD5 md5 = new MD5CryptoServiceProvider();
			TripleDES des = new TripleDESCryptoServiceProvider();
			des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
			des.IV = new byte[des.BlockSize / 8];
			return des;
		}
	}
}

